<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" default-expand-all @node-click="handleNodeClick" />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="sName">
            <el-input
                    v-model="queryParams.sName"
                    placeholder="请输入姓名"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="性别" prop="sSex">
            <el-select v-model="queryParams.sSex" placeholder="请选择性别" clearable size="small">
              <el-option
                      v-for="dict in sSexOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="年龄" prop="sAge">
            <el-input
                    v-model="queryParams.sAge"
                    placeholder="请输入年龄"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="联系电话" prop="sPhone">
            <el-input
                    v-model="queryParams.sPhone"
                    placeholder="请输入联系电话"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="证件号码" prop="sNumid">
            <el-input
                    v-model="queryParams.sNumid"
                    placeholder="请输入证件号码"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="舞蹈考级" prop="sExam">
            <el-select v-model="queryParams.sExam" placeholder="请选择舞蹈考级" clearable size="small">
              <el-option
                      v-for="dict in sExamOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="剩余学期" prop="sRemainderSemester">
            <el-input
                    v-model="queryParams.sRemainderSemester"
                    placeholder="请输入剩余学期"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="学员状态" prop="sStatus">
            <el-select v-model="queryParams.sStatus" placeholder="请选择学员状态" clearable size="small">
              <el-option
                      v-for="dict in sStatusOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['student:information:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                    type="success"
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['student:information:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['student:information:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                    type="warning"
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['student:information:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="学员编号" align="center" prop="sId" />
          <el-table-column label="姓名" align="center" prop="sName" >
            <template slot-scope="scope">
              <el-button
                      style="text-decoration: underline"
                      size="mini"
                      type="text"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['student:information:edit']"
              >{{scope.row.sName}}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="性别" align="center" prop="sSex" :formatter="sSexFormat" />
          <el-table-column label="年龄" align="center" prop="sAge" />
<!--          <el-table-column label="联系电话" align="center" prop="sPhone" />-->
<!--          <el-table-column label="证件号码" align="center" prop="sNumid" />-->
          <el-table-column label="舞蹈考级" align="center" prop="sExam" :formatter="sExamFormat" />
          <el-table-column label="剩余学期" align="center" prop="sRemainderSemester" />
          <el-table-column label="学员状态" align="center" prop="sStatus" :formatter="sStatusFormat" />
          <el-table-column label="所属班级" align="center" prop="dept.deptName" />
<!--          <el-table-column label="备注" align="center" prop="remark" />-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
<!--              <router-link-->
<!--                      size="mini"-->
<!--                      type="text"-->
<!--                      icon="el-icon-document"-->
<!--                      :to="'/student/information/data?sId=' + scope.row.sId" class="link-type">-->
<!--                <span>缴费记录</span>-->
<!--              </router-link>-->
              <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-document"
                      @click="goToRecord(scope.row.sId)"
              >缴费记录
              </el-button>
              <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['student:information:edit']"
              >修改</el-button>
              <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['student:information:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />
      </el-col>
    </el-row>


    <!-- 添加或修改学员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="sName">
          <el-input v-model="form.sName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.sSex">
            <el-radio
              v-for="dict in sSexOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="sAge">
          <el-input v-model="form.sAge" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="联系电话" prop="sPhone">
          <el-input v-model="form.sPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="证件号码" prop="sNumid">
          <el-input v-model="form.sNumid" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="舞蹈考级">
          <el-select v-model="form.sExam" placeholder="请选择舞蹈考级">
            <el-option
                    v-for="dict in sExamOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="剩余学期" prop="sRemainderSemester">
          <el-input v-model="form.sRemainderSemester" placeholder="请输入剩余学期" />
        </el-form-item>
        <el-form-item label="学员状态">
          <el-radio-group v-model="form.sStatus">
            <el-radio
              v-for="dict in sStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属班级">
          <treeselect v-model="form.sAttClassId" :options="deptOptions" :disable-branch-nodes="true" :show-count="true" placeholder="请选择所属班级" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInformation, getInformation, delInformation, addInformation, updateInformation, exportInformation } from "@/api/student/information";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Information",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学员信息表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      // 性别字典
      sSexOptions: [],
      // 舞蹈考级字典
      sExamOptions: [],
      // 学员状态字典
      sStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sName: null,
        sSex: null,
        sAge: null,
        sPhone: null,
        sNumid: null,
        sExam: null,
        sRemainderSemester: null,
        sStatus: null,
        sAttClassId: null,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        sName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("sys_user_sex").then(response => {
      this.sSexOptions = response.data;
    });
    this.getDicts("stu_dance_exam").then(response => {
      this.sExamOptions = response.data;
    });
    this.getDicts("stu_status").then(response => {
      this.sStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询学员信息列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.sAttClassId = data.id;
      this.getList();
    },
    // 性别字典翻译
    sSexFormat(row, column) {
      return this.selectDictLabel(this.sSexOptions, row.sSex);
    },
    // 舞蹈考级字典翻译
    sExamFormat(row, column) {
      return this.selectDictLabel(this.sExamOptions, row.sExam);
    },
    // 学员状态字典翻译
    sStatusFormat(row, column) {
      return this.selectDictLabel(this.sStatusOptions, row.sStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sName: null,
        sSex: "0",
        sAge: null,
        sPhone: null,
        sNumid: null,
        sExam: undefined,
        sRemainderSemester: null,
        sStatus: "0",
        sAttClassId: undefined,
        remark: null,
        delFlag: null,
        dataAttDept: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员信息";
    },
    /** 修改按钮操作 */
    goToRecord(sId) {
      this.$router.push({ path: '/student/information/data' , query: {'sId':sId}});
    },
    handleUpdate(row) {
      this.reset();
      const sId = row.sId || this.ids
      getInformation(sId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sId != null) {
            updateInformation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addInformation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sIds = row.sId || this.ids;
      this.$confirm('是否确认删除学员信息编号为"' + sIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInformation(sIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有学员信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInformation(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
